package com.ws.game.lesson3.fire
{
	import com.ws.game.common.framework.BaseGameObject;
	import com.ws.game.common.global.Global;
	import com.ws.game.common.utils.MoveDirectionType;
	import com.ws.game.common.utils.PartType;
	
	public class Bullet extends BaseGameObject
	{
		protected var _shooter:ActionObject;
		protected var _direction:uint;
		protected var _speed:Number = 15.2;
		
		public function Bullet(shooter:ActionObject)
		{
			super();
			_shooter = shooter;
			_direction = _shooter.direction;
			graphics.beginFill(0xFFFFFF);
			graphics.drawCircle( -4, -4, 4);
			graphics.endFill();
		}
		
		override public function doWork():void
		{
			switch(_direction)
			{
				case MoveDirectionType.UP:
					y -= _speed;					
					break;
				case MoveDirectionType.DOWN:
					y += _speed;
					break;
				case MoveDirectionType.LEFT:
					x -= _speed;
					break;
				case MoveDirectionType.RIGHT:
					x += _speed;
					break;
				default:break;
			}
			
			//check the touch
			for each(var obj:BaseGameObject in Global.scene.gObjects)
			{
				if (obj.hitTest && obj.hitTestPoint(x, y, true) && obj!=_shooter
				&& obj.part != _shooter.part)
				{
					if(obj.part == PartType.UNKNOWN){
						this.dispose();
						break;
					}else{
						if (!obj.hasOwnProperty('hurt'))
							continue;
						(obj as FaceObject).hurt(20);
						
						dispose();
						break;
					}
				}
			}
			
			//check the out of stage
			if (x<0 || x>Global.stage.stageWidth || y<0 || y>Global.stage.stageHeight)
				dispose();
		}
		
		override public function dispose():void{
			graphics.clear();
			Global.scene.removeGameObject(this);
		}
	}
}